using System;
using System.Collections.Generic;
using Common;
using DAL;
using Model;

namespace BLL {
    public class OrderProductBLL {
        private OrderProductDAL _orderProductDAL = new OrderProductDAL();
        public List<OrderProductDTO> QueryAllByOrderId(int orderId) {
            return _orderProductDAL.SelectAllByOrderId(orderId);
        }

        public void Add(OrderProduct orderProduct) {
            //如果存在则为添加数量
            int num = _orderProductDAL.SelectByOPID(orderProduct);
            if (num > 0) {
                //更新数量
                orderProduct.ProCount = num + 1;
                _orderProductDAL.Update(orderProduct);
            } else {
                //新增
                orderProduct.ProCount = 1;
                orderProduct.SubTime = DateTime.Now;
                _orderProductDAL.Insert(orderProduct);
            }
        }

        public Result<OrderProduct> Deduct(OrderProduct orderProduct) {
            //如果存在则为减少数量
            int num = _orderProductDAL.SelectByOPID(orderProduct);
            if (num > 1) {
                //更新数量
                orderProduct.ProCount = num - 1;
                _orderProductDAL.Update(orderProduct);
            } else {
                //逻辑删除
                orderProduct.ProCount = 0;
                orderProduct.SubTime = DateTime.Now;
                orderProduct.DelFlag = 1;
                _orderProductDAL.Delete(orderProduct);
            }

            return Result<OrderProduct>.Ok();
        }

        public void Update(int orderId) {
            _orderProductDAL.Checkout(orderId);
        }
    }
}